﻿using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using System.Windows.Input;
using AnyCAD.Foundation;
using System.Windows;
using AnyCAD.WPF.ViewModel;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System;
using AnyCAD.WPF.View;

namespace Rapid.CAX.ViewModel
{
    public class RapidMainViewModel : MainViewModel
    {
        // 文档的标识符
        public static readonly string DocumentType = "Rapid";

        public ICommand Save { get; set; }
        public ICommand Open { get; set; }
        public ICommand New { get; set; }
        public ICommand Undo { get; set; }
        public ICommand Redo { get; set; }

        public ListBox mOutputMessage;

        public RapidMainViewModel(ProjectBrowserViewModel projectViewModel, StackPanel propertyView, ListBox outputBox)
            :base(projectViewModel, propertyView)
        {
            Open = new RelayCommand(()=>OpenDocument());
            New = new RelayCommand(()=>NewDocument(DocumentType));
            Save = new RelayCommand(()=>OpenDocument());
            Undo = new RelayCommand(()=>OnUndo());
            Redo = new RelayCommand(()=>OnRedo());

            mOutputMessage = outputBox;

            OutputMessage("Me","Welcome to use RapidCAX!");

            if(!ProcessManager.Instance.Start("pclserver", @"PclServer.exe",
                string.Format("--hostId anycad.pcl --slaveId pcl.anycad"),
                OutputMessage))
            {
                OutputMessage("PclServer", "Failed to execute PclServer.");
            }
        }

        void AddOutputBox(string msg)
        {
            mOutputMessage.Items.Add(msg);
        }
        public void OutputMessage(string name, string? msg)
        {
            if (msg == null)
                return;

            System.Windows.Application.Current.Dispatcher.Invoke(AddOutputBox, msg);
        }

        public override void ProjectSelectionChanged()
        {
            base.ProjectSelectionChanged();
            // 目录树里面选择了对象，在这里处理
        }

        public override void ViewSelectionChanged(PickedResult picked)
        {
            base.ViewSelectionChanged(picked);
            // 三维视图里面选择了对象，在这里处理
        }
    }
}
